package edu.hainnu.niit.controller;

import edu.hainnu.niit.model.News;
import edu.hainnu.niit.model.PageBean;
import edu.hainnu.niit.model.Product;
import edu.hainnu.niit.model.auction;
import edu.hainnu.niit.service.IAuctionService;
import edu.hainnu.niit.service.INewsService;
import edu.hainnu.niit.service.IProductService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * TODO
 *
 * @package_name edu.hainnu.niit.controller
 * @auther GGB
 * @date 2021/12/12
 * @time 16:42
 */
@RestController
public class Paging {
    @Resource
    IProductService iProductService;
    @Resource
    INewsService iNewsService;
    @Resource
    IAuctionService iAuctionService;
    @GetMapping("paging/product")
    @ResponseBody
    public PageBean<Product> productPaging(String currentPageStr,String pageSizeStr,String sname){
        if(sname==null||sname.equals("undefined")){
            sname=null;
        }
        int currentPage=0;
        if(currentPageStr!=null&&currentPageStr.length()>0){
            currentPage = Integer.parseInt(currentPageStr);
        }else {
            currentPage=1;
        }
        int pageSize =0;
        if(pageSizeStr!=null&&pageSizeStr.length()>0){
            pageSize = Integer.parseInt(pageSizeStr);
        }else {
            pageSize=9;
        }

        PageBean<Product> productList = iProductService.queryProductsService(currentPage,pageSize,sname);
        return productList;
    }

    @GetMapping("paging/news")
    @ResponseBody
    public PageBean<News> newsPaging(String currentPageStr, String pageSizeStr,String type,String sname){
        if(sname==null||sname.equals("undefined")||sname.equals("null")){
            sname=null;
        }
        int currentPage=0;
        if(currentPageStr!=null&&currentPageStr.length()>0){
            currentPage = Integer.parseInt(currentPageStr);
        }else {
            currentPage=1;
        }
        int pageSize =0;
        if(pageSizeStr!=null&&pageSizeStr.length()>0){
            pageSize = Integer.parseInt(pageSizeStr);
        }else {
            pageSize=5;
        }
        PageBean<News> newsList;
        if(type==null||type.equals("")||type.equals("null")){
             newsList = iNewsService.queryNewsService(currentPage,pageSize,sname);
        }
        else {
            Integer t = Integer.parseInt(type);
             newsList = iNewsService.queryTypeNewsService(currentPage,pageSize,t,sname);
        }

        return newsList;
    }
    @GetMapping("paging/auction")
    @ResponseBody
    public PageBean<auction> auctionPaging(String currentPageStr, String pageSizeStr,String type,String sname){
        if(sname==null||sname.equals("undefined")||sname.equals("null")){
            sname=null;
        }
        int currentPage=0;
        if(currentPageStr!=null&&currentPageStr.length()>0){
            currentPage = Integer.parseInt(currentPageStr);
        }else {
            currentPage=1;
        }
        int pageSize =0;
        if(pageSizeStr!=null&&pageSizeStr.length()>0){
            pageSize = Integer.parseInt(pageSizeStr);
        }else {
            pageSize=9;
        }
        PageBean<auction> auctionList;
        if(type==null||type.equals("")||type.equals("null")||type.equals("undefined")){
         auctionList = iAuctionService.queryAuctionsService(currentPage,pageSize,sname);
        }
        else {
            auctionList = iAuctionService.queryTypeAuctionsService(currentPage,pageSize,type,sname);
        }
        return auctionList;
    }

}
